<?php

namespace app\modules\controllers;

use app\modules\models\Brand;
use app\modules\models\Category;
use app\modules\models\Order;
use app\modules\models\Parts;
use app\modules\models\Product;
use Yii;
use yii\data\Pagination;

class ProductController extends CommonController
{ 
    protected $mustlogin = ['list', 'add', 'mod', 'del','parts','partsadd','partsmod','partsdel'];

    public function actionList()
    {
        $this->layout = "layout1";
        $model = Product::find()->orderBy('id DESC');
        $count = $model->count();
        $pageSize = Yii::$app->params['pageSize']['manage'];
        $pager = new Pagination(['totalCount' => $count, 'pageSize' => $pageSize]);
        $data = $model->offset($pager->offset)->limit($pager->limit)->all();
        $categorylist = Category::getData();
        $brandlist = Brand::getData();
        foreach ($data as $k=>$v){
            $data[$k]->categoryid=$categorylist[$v->categoryid]?:'无';
            $data[$k]->brandid=$brandlist[$v->brandid]?:'无';
        }
        return $this->render("products", ['data' => $data, 'pager' => $pager]);
    }

    public function actionAdd()
    {
        $model = new Product();
        $this->layout = "layout1";
        if (Yii::$app->request->isPost) {
            $post = Yii::$app->request->post();
            if ($model->add($post)) {
                //Yii::$app->session->setFlash("info", "添加成功");
                $this->redirect(['product/list']);
            }
        }
        $categorylist = Category::getData();
        $brandlist = Brand::getData();
        return $this->render("add", ['model' => $model,'categorylist'=>$categorylist,'brandlist'=>$brandlist,'title'=>'添加产品']);
    }

    public function actionMod()
    {
        $this->layout = "layout1";
        $brandid = Yii::$app->request->get("id");
        $model = Product::find()->where('id = :id', [':id' => $brandid])->one();
        if (Yii::$app->request->isPost) {
            $post = Yii::$app->request->post();
            if ($model->load($post) && $model->save()) {
                //Yii::$app->session->setFlash('info', '修改成功');
                $this->redirect(['product/list']);
            }
        }
        $categorylist = Category::getData();
        $brandlist = Brand::getData();
        return $this->render('add', ['model' => $model,'categorylist'=>$categorylist,'brandlist'=>$brandlist,'title'=>'修改产品']);
    }

    public function actionDel()
    {
        try {
            $id = Yii::$app->request->get('id');
            if (empty($id)) {
                throw new \Exception('参数错误');
            }
            $data = Order::find()->where('productid = :pid', [":pid" => $id])->one();
            if ($data) {
                throw new \Exception('该产品下有关联订单，不允许删除');
            }
            if (!Product::deleteAll('id = :id', [":id" => $id])) {
                throw new \Exception('删除失败');
            }
            Yii::$app->session->setFlash('info', '删除成功');
        } catch(\Exception $e) {
            Yii::$app->session->setFlash('info', $e->getMessage());
        }
        return $this->redirect(Yii::$app->request->referrer);
    }

    public function actionParts(){
        $this->layout = "layout1";
        $productid = Yii::$app->request->get("productid");
        $model = Parts::find()->where('productid='.$productid)->orderBy('id DESC');
        $count = $model->where('productid='.$productid)->count();
        $pageSize = Yii::$app->params['pageSize']['manage'];
        $pager = new Pagination(['totalCount' => $count, 'pageSize' => $pageSize]);
        $data = $model->offset($pager->offset)->limit($pager->limit)->where('productid='.$productid)->all();
        $productid = Yii::$app->request->get("productid");
        $model_product=Product::find()->where('id = :id', [':id' => $productid])->one();
        return $this->render("parts",['data' => $data, 'pager' => $pager,'ptitle'=>$model_product->title]);
    }

    public function actionPartsadd(){
        $model=new Parts();
        $this->layout = "layout1";
        $productid = Yii::$app->request->get("productid");
        $model_product=Product::find()->where('id = :id', [':id' => $productid])->one();
        if (Yii::$app->request->isPost) {
            $post = Yii::$app->request->post();
            if ($model->add($post)) {
                Yii::$app->session->setFlash("info", "添加成功");
            }
        }
        return $this->render("partsadd",['model'=>$model,'ptitle'=>$model_product->title,'title'=>'添加新零件']);
    }

    public function actionPartsmod()
    {
        $this->layout = "layout1";
        $partsid = Yii::$app->request->get("partsid");
        $model = Parts::find()->where('id = :id', [':id' => $partsid])->one();
        if (Yii::$app->request->isPost) {
            $post = Yii::$app->request->post();
            if ($model->load($post) && $model->save()) {
                Yii::$app->session->setFlash('info', '修改成功');
            }
        }
        return $this->render('partsadd', ['model' => $model,'ptitle'=>Yii::$app->request->get('ptitle'),'title'=>'修改新零件']);
    }


    public function actionPartsdel()
    {
        try {
            $id = Yii::$app->request->get('partsid');
            if (empty($id)) {
                throw new \Exception('参数错误');
            }
            if (!Parts::deleteAll('id = :id', [":id" => $id])) {
                throw new \Exception('删除失败');
            }
            Yii::$app->session->setFlash('info', '删除成功');
        } catch(\Exception $e) {
            Yii::$app->session->setFlash('info', $e->getMessage());
        }
        return $this->redirect(Yii::$app->request->referrer);
    }


}